package com.xsz.pda.data.db

import androidx.room.Dao
import androidx.room.Insert
import androidx.room.Query
import androidx.room.Transaction
import androidx.room.Update
import com.xsz.pda.data.bean.Ticket
import com.xsz.pda.data.bean.TicketWithItem

@Dao
interface TicketDao {

    @Insert
    fun insertAll(vararg tickets: Ticket)

    @Insert
    fun insertAll(tickets: List<Ticket>)

    @Update
    fun update(vararg ticket: Ticket)

    @Update
    fun update(tickets: List<Ticket>)

    @Query("select * from ticket where delflag='0' and qrcode=:qrcode")
    fun findByQr(qrcode: String): Ticket?

    @Transaction
    @Query("select * from ticket where delflag='0' and qrcode=:qrcode")
    fun findByQrcode(qrcode: String): TicketWithItem?

    @Query("select * from ticket where id=:id")
    fun findById(id: String): Ticket?


    @Transaction
    @Query("select * from ticket where delflag='0' and m1=:m1")
    fun findByM1(m1: String): TicketWithItem?
    @Transaction
    @Query("select * from ticket limit 1")
    fun getFirst(): TicketWithItem?
    @Transaction
    @Query("select * from ticket where delflag='0' and idnumber=:idNumber limit 1")
    fun findByIdNumber(idNumber: String): TicketWithItem?

    @Query("select * from ticket where delflag='0' and idnumber like '%' || :idNumber || '%'")
    fun likeFindByIdNumber(idNumber: String): List<TicketWithItem>

    @Query("select * from ticket where delflag='0' and m1=:m1")
    fun findKeyValueByM1(m1: String): Ticket?

    @Transaction
    @Query("select * from ticket where delflag='0' and qrcode=:qrcode and m1=:m1")
    fun findByQrcodeAndM1(qrcode: String, m1: String): TicketWithItem?



    @Transaction
    @Query("select * from ticket where delflag='0' and idnumber=:idNumber and qrcode=:qrcode")
    fun findByIdNumberAndQrcode(qrcode: String, idNumber: String): TicketWithItem?

    @Query("select count(id) from ticket")
    fun getTicketSize(): Int?

    @Query("select id from ticket order by id desc limit 1")
    fun getLastId(): String?

    @Query("select timestamp from ticket order by timestamp desc limit 1")
    fun getLastSyncTime(): Long?

    @Query("delete from ticket")
    fun clear()

    @Query("select * from ticket")
    fun getAllFace(): List<Ticket>

    @Query("update ticket set delflag='1' where id=:id")
    fun logicDeleteById(id: String)
}